package com.kuanter.kuanterauto.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.kuanter.auto.sns.AccessTokenKeeper;
import com.kuanter.auto.sns.CallBack;
import com.kuanter.auto.sns.SNS;
import com.kuanter.auto.sns.TX;
import com.kuanter.auto.sns.XL;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.umeng.analytics.MobclickAgent;
/**
 * 绑定设置
 * @author kuanter
 *
 */
public class BoundSettingActivity extends BaseActivity_NoBar implements OnClickListener,
		CallBack {
	private Button back;
	private View xinlang_lock;
	private View tengxun_lock;
	private View weixin_lock;
	private boolean xinlang_flag = false;
	private boolean tengxun_flag = false;
	private boolean weixin_flag = false;
	private SNS xl;
	private SNS tx;
	private TextView xinlang_lock_tv;
	private TextView tengxun_lock_tv;
	private TextView weixin_lock_tv;
	
	public void onResume() {
		finishProgress();
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bound_setting);

		// 分享初始化
		xl = new XL(BoundSettingActivity.this, MainConstants.XL_KEY,
				MainConstants.XL_URL);
		xl.regCallback(this);
		tx = new TX(this, MainConstants.TX_KEY, MainConstants.TX_SCREAT,
				"null");
		tx.regCallback(this);
		
		tengxun_flag = AccessTokenKeeper.getTXToken(this);
		xinlang_flag = AccessTokenKeeper.getXLToken(this);
		
		back = (Button) this.findViewById(R.id.header_left);
		back.setOnClickListener(this);
		xinlang_lock = (View) this.findViewById(R.id.xinlang_lock);
		xinlang_lock.setOnClickListener(this);
		xinlang_lock_tv = (TextView)this.findViewById(R.id.xinlang_lock_tv);
		if (xinlang_flag) {
			if(!AccessTokenKeeper.getNickNameXL(BoundSettingActivity.this).equals("")) {
				xinlang_lock_tv.setText(AccessTokenKeeper.getNickNameXL(BoundSettingActivity.this));
			} else {
				xinlang_lock_tv.setText("已绑定");
			}
		} else {
			xinlang_lock_tv.setText("未绑定");
		}
		tengxun_lock = (View) this.findViewById(R.id.tengxun_lock);
		tengxun_lock.setOnClickListener(this);
		tengxun_lock_tv = (TextView)this.findViewById(R.id.tengxun_lock_tv);
		if (tengxun_flag) {
			if(!AccessTokenKeeper.getNickNameTX(BoundSettingActivity.this).equals("")) {
				tengxun_lock_tv.setText(AccessTokenKeeper.getNickNameTX(BoundSettingActivity.this));
			} else {
				tengxun_lock_tv.setText("已绑定");
			}
		} else {
			tengxun_lock_tv.setText("未绑定");
		}
		/*weixin_lock = (View) this.findViewById(R.id.weixin_lock);
		weixin_lock.setOnClickListener(this);
		weixin_lock_tv = (TextView)this.findViewById(R.id.weixin_lock_tv);
		if (weixin_flag) {
			weixin_lock_tv.setText("已绑定");
		} else {
			weixin_lock_tv.setText("未绑定");
		}*/
	}
	/**
	 * 处理各种点击事件
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_left:
			this.finish();
			break;
		case R.id.tengxun_lock:
			if (tengxun_flag) {
				new AlertDialog.Builder(BoundSettingActivity.this)
				.setMessage("确认要取消绑定腾讯微博吗?")
				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						tx.logOut();
						tengxun_flag = false;
						tengxun_lock_tv.setText("未绑定");
					}
				})
				.setNegativeButton("取消", null)
				.create().show();
			} else {
				showProgress("正在加载数据...");
				tx.setHandler(tx_handler);
				tx.oAuth();
			}
			break;
		/*case R.id.weixin_lock:
			if (weixin_flag) {
				weixin_flag = false;
				weixin_lock_tv.setText("未绑定");
			} else {
				weixin_flag = true;
				weixin_lock_tv.setText("已绑定");
			}
			break;*/
		case R.id.xinlang_lock:// 新浪锁
			if (xinlang_flag) {
				new AlertDialog.Builder(BoundSettingActivity.this)
				.setMessage("确认要取消绑定新浪微博吗?")
				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						xl.logOut();
						xinlang_flag = false;
						xinlang_lock_tv.setText("未绑定");
					}
				})
				.setNegativeButton("取消", null)
				.create().show();
			} else {
//				showProgress("正在加载数据...");
				xl.setHandler(xl_handler);
				xl.oAuth();
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 绑定腾讯响应
	 */
	Handler tx_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			finishProgress();
			if(TX.tx_success == msg.what) {
				tengxun_lock_tv.setText(AccessTokenKeeper.getNickNameTX(BoundSettingActivity.this));
			} else if(TX.tx_fail == msg.what) {
				tengxun_lock_tv.setText("已绑定");
			}
			super.handleMessage(msg);
		}
		
	};
	/**
	 * 绑定新浪响应
	 */
	Handler xl_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			finishProgress();
			if(XL.xl_success == msg.what) {
				xinlang_lock_tv.setText(AccessTokenKeeper.getNickNameXL(BoundSettingActivity.this));
			} else if(XL.xl_fail == msg.what){
				xinlang_lock_tv.setText("已绑定");
			}
			super.handleMessage(msg);
		}
		
	};
	
	@Override
	public void toShare(int index) {
		finishProgress();
		// TODO Auto-generated method stub
		if (index == CallBack.WHO_XL) {
			xinlang_flag = true;
//			xinlang_lock_tv.setText("已绑定");
		} else if (index == CallBack.WHO_TX) {
			tengxun_flag = true;
//			tengxun_lock_tv.setText("已绑定");
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		try {
			finishProgress();
			tx.activityResult(requestCode, resultCode, data);
		} catch (Exception e) {
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

}
